/**
 * @Author：ming
 * @Date：2023/10/4 6:36 PM
 * @Filename：Lc1208
 */

/**
 * @author ming
 */
public class Lc1208 {
    public int equalSubstring(String s, String t, int maxCost) {
        int len = s.length();
        int[] cost = new int[len];
        for (int i = 0; i < len; i++) {
            cost[i] = Math.abs( t.charAt(i)-s.charAt(i));
        }

        int max =0;
        int left =0;
        int right=0;
        int sum =0;
        while (right<len){
            sum+=cost[right];
            while (sum>maxCost){
                left++;
                sum -= cost[left];
            }
            max = Math.max(max,right-left+1);
            right++;
        }

        return max;
    }
}
